package javao.cn.work.orc.service.impl;

import javao.cn.work.orc.common.RectConstants;
import javao.cn.work.orc.pojo.survey.OrcSurvey;
import javao.cn.work.orc.service.SelectTestService;
import javao.cn.work.orc.utils.orc.OcrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 选择性定测ServiceImpl
 * @Author Admin小闫
 * @Date 2024/12/3
 */
@Service
public class SelectTestServiceImpl implements SelectTestService {

    @Autowired
    private OcrUtils ocrUtils;




    /**
     * 传入图片流识别 选择测量值
     * @param images 图片流
     * @return 识别结果
     */
    @Override
    public OrcSurvey orcSurvey(Map<Integer, BufferedImage> images){
        OrcSurvey orcSurvey=new OrcSurvey();
        List<Map<String, Map<String, Map<String,String>>>> list=new ArrayList<>();
        for (Map.Entry<Integer, BufferedImage> entry : images.entrySet()) {
            Integer key = entry.getKey();
            BufferedImage Image = entry.getValue();
            //首页的图片格式
            if (key==0){
                //取出首页的6个值
                orcSurvey.setPartName(ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_PART_NAME));
                orcSurvey.setPartNo(ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_PART_NO));
                orcSurvey.setPartCounter(ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_PART_COUNTER));
                orcSurvey.setOperator(ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_OPERATOR));
                orcSurvey.setOperator_AF(ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_OPERATOR_AF));
                orcSurvey.setMachine(ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_MACHINE));
                //读首页的两组数值
                Map<String, Map<String, Map<String,String>>>  stringOrcRzMap = orcReadValue(Image, 0);
                if (stringOrcRzMap.size()<1){
                    break;
                }
                list.add(stringOrcRzMap);
                Map<String, Map<String, Map<String,String>>>  stringOrcRzMap1 = orcReadValue(Image, 1);
                if (stringOrcRzMap1.size()<1){
                    break;
                }
                list.add(stringOrcRzMap1);
            }
            if (key>0){
                Map<String, Map<String, Map<String,String>>>  stringOrcRzMap2 = orcReadValue(Image, 2);
                if (stringOrcRzMap2.size()<1){
                    break;
                }
                list.add(stringOrcRzMap2);
                Map<String, Map<String, Map<String,String>>>  stringOrcRzMap3 = orcReadValue(Image, 3);
                if (stringOrcRzMap3.size()<1){
                    break;
                }
                list.add(stringOrcRzMap3);
            }
        }
        orcSurvey.setSurveyValue(list);
        return orcSurvey;
    }

    /**
     * 读取每组值
     * @param Image
     * @param i
     * @return
     */
    private Map<String, Map<String, Map<String,String>>> orcReadValue(BufferedImage Image, int i){
        Map<String, Map<String, Map<String,String>>> map=new HashMap<>();
        Map<String, Map<String,String>> af90RzMap=new HashMap<>();
        String s = ocrUtils.performOCRByRegion(Image, RectConstants.SELECT_TEST_VALUE[i][0]);
        if (s==null||s.equals("")){//如果没识别到组号，则代表本页结束了，不需要往下识别返回空集合
            return map;
        }
        Map<String,String> orcRz=new HashMap<>();
        orcRz.put("act",ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_VALUE[i][1]));
        orcRz.put("lt",ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_VALUE[i][2]));
        orcRz.put("ut",ocrUtils.performOCRByRegion(Image,  RectConstants.SELECT_TEST_VALUE[i][3]));
        af90RzMap.put("RZ",orcRz);
        map.put(s,af90RzMap);
        return map;
    }


}
